﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using QuanLyHocPhi.Entity;
using QuanLyHocPhi.DAL;
using QuanLyHocPhi.Util;

namespace QuanLyHocPhi
{
    public partial class frmPhidoituong : DevComponents.DotNetBar.Metro.MetroForm
    {
        DOI_TUONG_DAO dtDao = new DOI_TUONG_DAO();
        DOI_TUONG doiTuongDuocChon = new DOI_TUONG();
        DOI_TUONG dt = new DOI_TUONG();
        MUC_PHI mp = new MUC_PHI();
        public frmPhidoituong()
        {
            InitializeComponent();
        }

       

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (txtTen.Text == "")
            {
                errorInfo.SetError(txtTen, "Yêu cầu nhập tên đối tượng");
                return;
            }
            
            dt.TEN_DOI_TUONG = txtTen.Text;
            dt.LOP = txtLoailop.Text;
            dt.GHI_CHU = txtGhichu.Text;

            if (dtDao.Insert(dt))
            {
                MessageBox.Show("Thêm thành công", "Thông báo");
            }
            else {
             
                
                    MessageBox.Show("Vui lòng nhập lại", "Thông báo");
                
            }
            frmPhidoituong_Load(sender, e);
        }
        IList<DOI_TUONG> ds = null;
        private void frmPhidoituong_Load(object sender, EventArgs e)
        {

            IList<string> dsDoiTuong = dtDao.GetAllObjectLOP();
            foreach (string dt in dsDoiTuong)
            {
                cbblopdoituong.Items.Add(dt);
            }
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            
            dt.TEN_DOI_TUONG = txtTen.Text;
            dt.LOP = txtLoailop.Text;
            dt.GHI_CHU = txtGhichu.Text;

            dtDao.Update(dt);
            frmPhidoituong_Load(sender, e);
         
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            

            dtDao.Delete(dt.MA_DOI_TUONG);
            txtLoailop.Text = "";
            txtGhichu.Text = "";
            txtTen.Text = "";
            frmPhidoituong_Load(sender, e);
            

          
        }

        private void cbblopdoituong_SelectedIndexChanged(object sender, EventArgs e)
        {
            string lop = cbblopdoituong.SelectedItem.ToString();
            if (lop != null)
            {
              ds= dtDao.GetObjectsByLOP(lop);
                dtgvDoituong.AutoGenerateColumns = false;
                dtgvDoituong.DataSource = ds;
            }
        }

        private void dtgvDoituong_SelectionChanged(object sender, EventArgs e)
        {
           
        }

        private void dtgvDoituong_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtTen.Text = ds[e.RowIndex].TEN_DOI_TUONG;
            txtLoailop.Text = ds[e.RowIndex].LOP;
            txtGhichu.Text = ds[e.RowIndex].GHI_CHU;
            doiTuongDuocChon = ds[e.RowIndex];
            dt = ds[e.RowIndex];
            
        }

        private void buttonX1_Click_1(object sender, EventArgs e)
        {
           //kiem tra 1 row duoc chon
           
            //.
            //tiep theo
            int maDoiTuong = doiTuongDuocChon.MA_DOI_TUONG;
            frmMucphi frm = new frmMucphi(maDoiTuong);
            
            frm.Show();
          
        }

        private void dtgvDoituong_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}